﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using ModularAndPlugin.WinApp.Contracts;
using SailHero.Soft.FrameWork.IDomainRepositories.Contracts;
using SailHero.Soft.FrameWork.Models;

namespace UI.WinForm.Clients
{
    public class ClientsService
    {
        IDataModule _dataModule;

        public ClientsService()
        {
            _dataModule = ClientsCommandFacade.ModuleHandler.DataModule;
        }

        public TB_CLIENTS GetClient(int id)
        {
            try
            {

                var source = _dataModule.tB_CLIENTSRepository.GetList(itm => itm.ID == id);
                if (source != null && source.Count() > 0)
                {
                    return source.FirstOrDefault();
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            return null;
        }

        public string GetClientName(int id)
        {
            try
            {
                var source = _dataModule.tB_CLIENTSRepository.GetList(itm => itm.ID == id);
                if (source != null && source.Count() > 0)
                {
                    return source.FirstOrDefault().MCNNAME;
                }
                else
                    return "";
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public List<TB_CLIENTS> GetClientsDataTable()
        {
            try
            {
                var source = _dataModule.tB_CLIENTSRepository.GetList(itm => itm.ID > 0);
                if (source != null && source.Count() > 0)
                {
                    return source.ToList();
                }
                else
                    return null;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public int Insert(string MN_, DateTime DT_, string MCNName)
        {
            try
            {
                TB_CLIENTS mdl = new TB_CLIENTS() { MN = MN_, DT = DT_, MCNNAME = MCNName };
                _dataModule.tB_CLIENTSRepository.Add(mdl);
                _dataModule.tB_CLIENTSRepository.UnitOfWork.SaveChanges();
                return mdl.ID;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        private int NewID()
        {
            return 0;

        }
    }
}